//
//  XMConversationViewController.m
//  XunMi
//
//  Created by MacBook on 2024/4/7.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMConversationViewController.h"
#import "XMReportViewController.h"
#import "XMUserDetailViewController.h"
#import "XMRechargeViewController.h"
#import "XMConversationBgController.h"
#import "XMRealViewController.h"
#import "XMConversationMoreView.h"
#import "XMUserDetailRemarkView.h"
#import "XMConversationNoticeView.h"
#import "XMConversationInfoView.h"
#import "XMGiftView.h"
#import "XMConversationInviteView.h"
#import "XMSVGAPlayView.h"
#import "XMOpenGuardTipView.h"
#import "XMCancelHideTipView.h"
#import "XMConOpenBoxView.h"
#import "XMConOpenBoxResultView.h"
#import "XMHasGuardTipView.h"
#import "XMRealTipView.h"
#import "XMCustomSendCell.h"
#import "XMSendInviteMsgCell.h"
#import "XMInviteMessage.h"
#import "XMCustomSendMessage.h"
#import <RongIMLib/RongIMLib.h>
#import <RongCallLib/RongCallLib.h>
#import "RCCall.h"
#import "XMRCManager.h"
#import "XMGiftModel.h"
#import "XMOpenboxModel.h"
#import "XMBagModel.h"
#import "XMRTCConfigModel.h"

@interface XMConversationViewController ()
@property (nonatomic, strong) XMConversationMoreView *moreView;
@property (nonatomic, strong) XMUserDetailRemarkView *remarkView;
@property (nonatomic, strong) XMConversationNoticeView *noticeView;
@property (nonatomic, strong) XMConversationInfoView *infoView;
@property (nonatomic, strong) XMGiftView *giftView;
@property (nonatomic, strong) XMConversationInviteView *inviteView;
@property (nonatomic, strong) XMSVGAPlayView *svgaPlayView;
@property (nonatomic, strong) XMOpenGuardTipView *openGuardTipView;
@property (nonatomic, strong) XMCancelHideTipView *cancelHideTipView;
@property (nonatomic, strong) XMConOpenBoxView *openBoxView;
@property (nonatomic, strong) XMConOpenBoxResultView *boxResultView;
@property (nonatomic, strong) XMHasGuardTipView *hasGuardView;
@property (nonatomic, strong) XMRealTipView *realTipView;
@property (nonatomic, strong) UIImageView *bgImgView;
@property (nonatomic, strong) XMUserModel *userModel;
@property (nonatomic, strong) XMRTCConfigModel *rtcModel;
@property (nonatomic, assign) CGFloat noticeViewH;
@property (nonatomic, assign) CGFloat infoViewH;
@property (nonatomic, strong) NSMutableArray *reply_message;
@end

@implementation XMConversationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initVariables];
    [self setupSubviews];
    [self userObtain];
    [self rtcData];
    [self giftData];
    [self bagData];
    [self openboxRuleData];
    [self viewBlock];
    [self reloadFreeMsgCount];
    [self.view bringSubviewToFront:self.navView];
    [kNotificationCenter addObserver:self selector:@selector(handleCallReloadNotification:) name:@"callreload" object:nil];
    
    [kNotificationCenter addObserver:self selector:@selector(muted) name:@"xunmi_muted" object:nil];
}

- (void)handleCallReloadNotification:(NSNotification *)notification {
    [self.conversationDataRepository enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        RCMessageModel *model = obj;
        if ([model.messageUId isEqualToString:notification.object]) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:idx inSection:0];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.conversationMessageCollectionView reloadItemsAtIndexPaths:@[indexPath]];
            });
            *stop = YES;
        }
    }];
}

- (void)muted {
    [self userObtain];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if ([GKCover hasCover]) {
        [GKCover hideCover];
    }
}

#pragma mark - initData
- (void)userObtain {
    if (self.targetId.length > 0) {
        NSMutableDictionary *dic = @{
            @"see_user_id": self.targetId,
            @"module": @"8"
        }.mutableCopy;
        [YHNetworkManager.sharedInstance postWithUrl:@"api/user/obtain" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
            if (tip.success) {
                XMUserModel *model = [XMUserModel mj_objectWithKeyValues:result];
                self.userModel = model;
                self.infoView.model = model;
                
                [self.bgImgView sd_setImageWithURL:kStringToUrl(model.chat_cover)];
                [kGlobalVariables.chatBgImgDic setObject:model.chat_cover forKey:model.user_id];

                [self.moreView.blackButton setTitle:model.is_block ? @"取消拉黑" : @"拉黑" forState:UIControlStateNormal];
                
                [self.moreView.hideButton setTitle:model.is_hidden ? @"取消对其隐身" : @"在线对其隐身" forState:UIControlStateNormal];
                
                [[XMRCManager sharedInstance] updateUserCacheUserId:model.user_id alias:model.alias name:model.nickname portraitUri:model.avatar.file_url extra:[XMUtil extraJsonMember_time:model.member_time intimacy:model.intimacy]];
                [self mineGolds];
            }
        }];
    }
}

- (void)mineGolds {
    NSMutableDictionary *dic = @{
        @"module": @"4"
    }.mutableCopy;
    [YHNetworkManager.sharedInstance postWithUrl:@"api/user/obtain" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        if (tip.success) {
            XMUserModel *model = [XMUserModel mj_objectWithKeyValues:result];
            kUserModel.golds = model.golds;
            self.userModel.invite_state = model.invite_state;
        }
    }];
}

- (void)rtcData {
    NSMutableDictionary *dic = @{
        @"target_user_id": self.targetId,
    }.mutableCopy;
    [YHNetworkManager.sharedInstance postWithUrl:@"api/chat/rtc/data" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        if (tip.success) {
            self.rtcModel = [XMRTCConfigModel mj_objectWithKeyValues:result];
        }
    }];
}

- (void)giftData {
    NSMutableDictionary *dic = @{
        @"module": @"1"
    }.mutableCopy;
    [YHNetworkManager.sharedInstance postWithUrl:@"api/config/giftConfig" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, NSDictionary * _Nullable result) {
        if (tip.success) {
            self.giftView.giftArray = [XMGiftModel mj_objectArrayWithKeyValuesArray:[result objectForKey:@"record"]];
        }
    }];
    
    NSMutableDictionary *dic2 = @{
        @"module": @"2"
    }.mutableCopy;
    [YHNetworkManager.sharedInstance postWithUrl:@"api/config/giftConfig" parameters:dic2 completeBlock:^(XMResponseTip * _Nonnull tip, NSDictionary * _Nullable result) {
        if (tip.success) {
            self.giftView.memberArray = [XMGiftModel mj_objectArrayWithKeyValuesArray:[result objectForKey:@"record"]];
        }
    }];
}

- (void)bagData {
    NSMutableDictionary *dic = @{}.mutableCopy;
    [YHNetworkManager.sharedInstance postWithUrl:@"api/chat/knapsack/list" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        if (tip.success) {
            NSArray <XMBagModel *> *array = [XMBagModel mj_objectArrayWithKeyValuesArray:result[@"record"]];
            self.giftView.bagArray = array;
        }
    }];
}

- (void)openboxRuleData {
    NSMutableDictionary *dic = @{}.mutableCopy;
    [YHNetworkManager.sharedInstance postWithUrl:@"api/activity/openBlindBox/detail" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        if (tip.success) {
            self.openBoxView.model = [XMOpenboxModel mj_objectWithKeyValues:result];
        }
    }];
}

#pragma mark - 送礼物
- (void)sendGift:(NSString *)gift_id
    animationUrl:(NSString *)animationUrl
        complete:(void(^)(void))complete{
    [MBProgressHUD showHUDActivityMsg:nil toView:kWindow];
    NSMutableDictionary *dic = @{
        @"gift_id": gift_id,
        @"target_user_id": self.targetId
    }.mutableCopy;
    [YHNetworkManager.sharedInstance postWithUrl:@"api/chat/send/gift" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id result) {
        if (tip.success) {
            [MBProgressHUD hideHUD];
            XMGiftModel *giftModel = [XMGiftModel mj_objectWithKeyValues:result];
            if (tip.success) {
                XMWeakSelf(self);
                if ([GKCover hasCover]) {
                    [GKCover hideCoverWithHideBlock:^{
                        XMStrongSelf(self);
                        kUserModel.golds = giftModel.user_golds;
                        [self sendGiftMessage:giftModel];
                        if (!self.userModel.is_hidden && ![XMUtil isBlankString:animationUrl]) {
                            [self showGiftSvgaPlayView:animationUrl finishBlock:^(SVGAPlayer *player) {
                                [GKCover hideCover];
                            }];
                        }
                    }];
                } else {
                    kUserModel.golds = giftModel.user_golds;
                    [self sendGiftMessage:giftModel];
                    if (!self.userModel.is_hidden && ![XMUtil isBlankString:animationUrl]) {
                        [self showGiftSvgaPlayView:animationUrl finishBlock:^(SVGAPlayer *player) {
                            [GKCover hideCover];
                        }];
                    }
                }
                if (complete) {
                    complete();
                }
            }
        }
    }];
}

#pragma mark - 开盲盒
- (void)openboxDataComplete:(void(^)(void))complete {
    if (![self checkBalanceEnough:kGlobalVariables.appConfig.chat.blind_box_golds message:kGlobalVariables.appConfig.chat.otherMsgTipStr isGift:NO]) {
        return;
    }
    [MBProgressHUD showHUDActivityMsg:nil toView:kWindow];
    NSMutableDictionary *dic = @{
        @"target_user_id": self.userModel.user_id
    }.mutableCopy;
    [YHNetworkManager.sharedInstance postWithUrl:@"api/activity/openBlindBox/handle" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        if (tip.success) {
            [MBProgressHUD hideHUD];
            XMGiftModel *model = [XMGiftModel mj_objectWithKeyValues:result];
            kUserModel.golds = model.user_golds;
            XMWeakSelf(self);
            [self.svgaPlayView playWithSource:@"box"
                                  finishBlock:^(SVGAPlayer *_Nonnull player) {
                XMStrongSelf(self);
                XMWeakSelf(self);
                [GKCover hideCoverWithHideBlock:^{
                    XMStrongSelf(self);
                    [self sendGiftMessage:model];
                    self.boxResultView.model = model;
                    [self showBoxResultView];
                }];
            } faileBlock:^(NSError *_Nullable error){}];
            [GKCover coverFrom:kWindow contentView:self.svgaPlayView style:GKCoverStyleTransparent showStyle:GKCoverShowStyleCenter showAnimStyle:GKCoverShowAnimStyleNone hideAnimStyle:GKCoverHideAnimStyleNone notClick:YES];
            if (complete) {
                complete();
            }
        }
    }];
}

#pragma mark - 开守护
- (void)openGuardDataComplete:(void(^)(void))complete {
    if (![self checkBalanceEnough:kGlobalVariables.appConfig.chat.user_guard_golds message:kGlobalVariables.appConfig.chat.otherMsgTipStr isGift:NO]) {
        return;
    }
    [MBProgressHUD showHUDActivityMsg:nil toView:kWindow];
    NSMutableDictionary *dic = @{
        @"target_user_id": self.userModel.user_id
    }.mutableCopy;
    [YHNetworkManager.sharedInstance postWithUrl:@"api/chat/open/guard" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        if (tip.success) {
            [MBProgressHUD hideHUD];
            kUserModel.golds = [result toString:@"user_golds"];
            XMCustomSendMessage *message = [XMCustomSendMessage guardMessageWithName:@"守护" golds:kGlobalVariables.appConfig.chat.user_guard_golds score:kStringFormat(@"%.2f", [result toFloat:@"give_score"]) msgType:@(2)];
            [self sendMessage:message pushContent:nil];
            if (complete) {
                complete();
            }
        }
    }];
}

- (void)xm_sendMessageType:(XMChatMessageSendType)type
                  complete:(void (^)(BOOL success))complete {
    NSMutableDictionary *dic = @{
        @"message_type": @(type),
        @"target_user_id": self.userModel.user_id
    }.mutableCopy;
    
    [YHNetworkManager.sharedInstance postWithUrl:@"api/chat/message/send" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        if (tip.success) {
            NSString *intimacy = [result toString:@"intimacy"];
            NSString *user_golds = [result toString:@"user_golds"];
            kUserModel.golds = user_golds;
            kUserModel.intimacy = intimacy;
            self.infoView.intimacy = intimacy;
            //            [self reloadFreeMsgCount];
            [[XMRCManager sharedInstance] updateUserCacheUserId:self.userModel.user_id alias:@"" name:@"" portraitUri:@"" extra:[XMUtil extraJsonMember_time:kUserModel.member_time intimacy:kUserModel.intimacy]];
        }
        complete(tip.success);
    }];
}

- (void)sendGiftMessage:(XMGiftModel *)model {
    XMCustomSendMessage *message = [XMCustomSendMessage giftMessageWithName:model.name golds:model.golds score:model.give_score.stringValue msgType:@(1) number:@(1) isVideo:@(0) index:model.index animUrl:model.animation.file_url imgUrl:model.image.file_url];
    [self sendMessage:message pushContent:nil];
}

#pragma mark - 初始化变量
- (void)initVariables {
    self.noticeViewH = 40.f;
    self.infoViewH = 130.f;
}
#pragma mark - setupSubViews
- (void)setupSubviews {
    // 显示用户昵称
    self.displayUserNameInCell = NO;
    
    self.view.backgroundColor = kHEX(@"F2F2F2");
    [self.navLeft2Btn setTitle:self.titleStr forState:UIControlStateNormal];
    
    if (self.conversationType == ConversationType_PRIVATE) {
        [self.navRightBtn setImage:kGetImage(@"user_d_more") forState:UIControlStateNormal];
        [self.navRightBtn addTarget:self action:@selector(more) forControlEvents:UIControlEventTouchUpInside];
        self.bgImgView = [[UIImageView alloc] initWithImage:[UIImage imageWithColor:kHEXA(@"000000", 0.06)]];
        [self.view insertSubview:self.bgImgView atIndex:0];
        [self.bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
        }];
        if (kGlobalVariables.chatBgImgDic[self.targetId]) {
            NSString *url = kGlobalVariables.chatBgImgDic[self.targetId];
            self.bgImgView.image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:url];
        };
        
        self.noticeView = [XMConversationNoticeView viewFromXib];
        [self.view addSubview:self.noticeView];
        
        self.infoView = [XMConversationInfoView viewFromXib];
        [self.view addSubview:self.infoView];
        
        [self updateViewLayout];
    } else {
        self.conversationMessageCollectionView.frame = CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight - kNavBarAndStatusBarHeight);
    }
}

- (void)updateViewLayout {
    self.noticeView.frame = CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, self.noticeViewH);
    
    self.infoView.frame = CGRectMake(0, kNavBarAndStatusBarHeight + self.noticeViewH + 20, kScreenWidth, self.infoViewH);
    
    self.conversationMessageCollectionView.frame = CGRectMake(0, kNavBarAndStatusBarHeight + self.noticeViewH + self.infoViewH + 20, kScreenWidth, kScreenHeight - (kNavBarAndStatusBarHeight + self.noticeViewH + self.infoViewH + 20 + self.chatSessionInputBarControl.frame.size.height + kBottomSafeHeight));
}

#pragma mark - viewBlock
- (void)viewBlock {
    XMWeakSelf(self);
#pragma mark - 底部按钮工具
    self.toolsView.clickBlock = ^(XMConversationToolsType type) {
        XMStrongSelf(self);
        if (kUserModel.isGirl && !kUserModel.idcard_verify) {
            [self showRealTipView];
            return;
        }
        
        [self xm_resignFirstResponder];
        switch (type) {
            case XMConversationToolsGuard: {
                if (kUserModel.isMan) {
                    if (self.userModel.is_watch) {
                        [self showhasGuardView];
                    } else {
                        [self showOpenGuardTipView];
                    }
                } else {
                    if (self.userModel.invite_state) {
                        if (self.userModel.is_watch) {
                            [self showhasGuardView];
                        } else {
                            [self showOpenGuardTipView];
                        }
                    } else {
                        [self showinviteViewType:XMConversationInviteGuard];
                    }
                }
            } break;
                
            case XMConversationToolsAudio: {
                if (kUserModel.isGirl) {
                    if (!self.userModel.rtc_status) {
                        [MBProgressHUD showHUDMsg:kStringFormat(@"亲密度大于%ld才可以语音通话哦", self.rtcModel.intimacy) toView:nil];
                        return;
                    }
                }
                
                if (![self checkBalanceEnough:@(self.rtcModel.voice_golds) message:kStringFormat(@"语音通话%@金币/分钟", @(self.rtcModel.voice_golds)) isGift:NO]) {
                    return;
                }
                
                [self.chatSessionInputBarControl openDynamicFunction:PLUGIN_BOARD_ITEM_VOIP_TAG];
                
            } break;
                
            case XMConversationToolsVideo: {
                if (kUserModel.isGirl) {
                    if (!self.userModel.rtc_status) {
                        [MBProgressHUD showHUDMsg:kStringFormat(@"亲密度大于%ld才可以视频通话哦", self.rtcModel.intimacy) toView:nil];
                        return;
                    }
                }
                
                if (![self checkBalanceEnough:@(self.rtcModel.video_golds) message:kStringFormat(@"对方设置视频通话为%@金币/分钟", @(self.rtcModel.video_golds)) isGift:NO]) {
                    return;
                }
                
                [self.chatSessionInputBarControl openDynamicFunction:PLUGIN_BOARD_ITEM_VIDEO_VOIP_TAG];
            } break;
                
            case XMConversationToolsGift: {
                if (kUserModel.isMan) {
                    self.giftView.isInvite = NO;
                    [self showGiftView];
                } else {
                    if (self.userModel.invite_state) {
                        self.giftView.isInvite = NO;
                        [self showGiftView];
                    } else {
                        [self showinviteViewType:XMConversationInviteGift];
                    }
                }
            } break;
                
            case XMConversationToolsBox: {
                if (kUserModel.isMan) {
                    [self showOpenBoxView];
                } else {
                    if (self.userModel.invite_state) {
                        [self showOpenBoxView];
                    } else {
                        [self showinviteViewType:XMConversationInviteBox];
                    }
                }
            } break;
                
            default:
                break;
        }
    };
    
#pragma mark - 右上角更多
    self.moreView.clickBlock = ^(XMConversationMoreType type) {
        XMLog(@"%ld", type);
        XMStrongSelf(self);
        switch (type) {
            case XMConversationMoreChatBg: {
                XMWeakSelf(self);
                [GKCover hideCoverWithHideBlock:^{
                    XMStrongSelf(self);
                    XMConversationBgController *vc = [[XMConversationBgController alloc] init];
                    vc.target_user_id = self.userModel.user_id;
                    [self.navigationController pushViewController:vc animated:YES];
                    
                    XMWeakSelf(self);
                    vc.finishBlock = ^{
                        XMStrongSelf(self);
                        [self userObtain];
                    };
                }];
            } break;
            case XMConversationMoreRemark: {
                XMWeakSelf(self);
                [GKCover hideCoverWithHideBlock:^{
                    XMStrongSelf(self);
                    [self showRemarkView];
                }];
            } break;
                
            case XMConversationMoreClear: {
                XMWeakSelf(self);
                [GKCover hideCoverWithHideBlock:^{
                    XMStrongSelf(self);
                    [self clearRecordAlert];
                }];
            } break;
                
            case XMConversationMoreHide: {
                XMWeakSelf(self);
                [GKCover hideCoverWithHideBlock:^{
                    XMStrongSelf(self);
                    [self setupHiding];
                }];
            } break;
                
            case XMConversationMoreBlack: {
                XMWeakSelf(self);
                [GKCover hideCoverWithHideBlock:^{
                    XMStrongSelf(self);
                    [self setupBlack];
                }];
            } break;
                
            case XMConversationMoreReport: {
                XMWeakSelf(self);
                [GKCover hideCoverWithHideBlock:^{
                    XMStrongSelf(self);
                    XMReportViewController *vc = [[XMReportViewController alloc] initWithNibName:kStringFromClass(XMReportViewController) bundle:nil];
                    vc.user_id = self.userModel.user_id;
                    [self.navigationController pushViewController:vc animated:YES];
                }];
            } break;
                
            case XMConversationMoreClose: {
                [GKCover hideCover];
            } break;
                
            default:
                break;
        }
    };
    
#pragma mark - 备注
    self.remarkView.sureBlock = ^(NSString * _Nonnull str) {
        XMStrongSelf(self);
        [MBProgressHUD showHUDActivityMsg:nil toView:kWindow];
        NSMutableDictionary *dic = @{
            @"target_user_id": [NSString xm_stringNoNullWith:self.userModel.user_id],
            @"remark": [NSString xm_stringNoNullWith:str]
        }.mutableCopy;
        [YHNetworkManager.sharedInstance postWithUrl:@"api/user/remark" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
            if (tip.success) {
                [MBProgressHUD hideHUD];
                [GKCover hideCover];
                [self.navLeft2Btn setTitle:![XMUtil isBlankString:str] ? str : self.userModel.nickname forState:UIControlStateNormal];
            }
        }];
    };
    
    self.remarkView.closeBlock = ^{
        [GKCover hideCover];
    };
    
#pragma mark - 滚动通知
    self.noticeView.closeBlock = ^{
        XMStrongSelf(self);
        [self.noticeView removeFromSuperview];
        self.noticeViewH = 0.f;
        [self updateViewLayout];
    };
    
#pragma mark - 用户信息
    self.infoView.expandBlock = ^(BOOL selected) {
        XMStrongSelf(self);
        self.infoViewH = selected ? 56.f : 130.f;
        [self updateViewLayout];
    };
    
#pragma mark - 礼物
    self.giftView.sendBlock = ^(XMGiftModel * _Nonnull model, BOOL isMember, BOOL isInvite) {
        XMStrongSelf(self);
        if (isInvite) {
            XMWeakSelf(self);
            [GKCover hideCoverWithHideBlock:^{
                XMStrongSelf(self);
                XMInviteMessage *message = [XMInviteMessage giftMessageWithName:model.name golds:model.golds score:model.give_score.stringValue msgType:@(1) number:@(1) isVideo:@(0) index:model.index animUrl:model.animation.file_url imgUrl:model.image.file_url];
                [self sendMessage:message pushContent:nil];
            }];
            
        } else {
            if (![self checkBalanceEnough:model.golds message:kGlobalVariables.appConfig.chat.otherMsgTipStr isGift:YES]) {
                return;
            }
            [self sendGift:kStringFormat(@"%@", model.index) animationUrl:model.animation.file_url complete:^{
            }];
        }
    };
    
#pragma mark - 守护
    self.openGuardTipView.guardBlock = ^{
        XMStrongSelf(self);
        XMWeakSelf(self);
        [GKCover hideCoverWithHideBlock:^{
            XMStrongSelf(self);
            [self openGuardDataComplete:^{
            }];
        }];
    };
    
    self.openGuardTipView.closeBlock = ^{
        [GKCover hideCover];
    };
    
    self.hasGuardView.guardBlock = ^{
        XMStrongSelf(self);
        XMWeakSelf(self);
        [GKCover hideCoverWithHideBlock:^{
            XMStrongSelf(self);
            [self openGuardDataComplete:^{
            }];
        }];
    };
    
    self.hasGuardView.closeBlock = ^{
        [GKCover hideCover];
    };
    
    self.infoView.tapBlock = ^(XMUserModel * _Nonnull model) {
        XMStrongSelf(self);
        XMUserDetailViewController *vc = [[XMUserDetailViewController alloc] init];
        vc.userId = self.userModel.user_id;
        vc.fromChat = YES;
        [self.navigationController pushViewController:vc animated:YES];
    };
    
    self.infoView.qinmiBlock = ^{
        XMStrongSelf(self);
        XMWebViewController *vc = [[XMWebViewController alloc] init];
        vc.titleStr = @"亲密度";
        vc.urlStr = kGlobalVariables.appConfig.intimacy_page;
        vc.needNav = YES;
        [self.navigationController pushViewController:vc animated:YES];
    };
    
    self.openBoxView.openBlock = ^{
        XMStrongSelf(self);
        XMWeakSelf(self);
        [GKCover hideCoverWithHideBlock:^{
            XMStrongSelf(self);
            [self openboxDataComplete:^{
            }];
        }];
    };
    
    self.boxResultView.openBlock = ^{
        XMStrongSelf(self);
        XMWeakSelf(self);
        [GKCover hideCoverWithHideBlock:^{
            XMStrongSelf(self);
            [self openboxDataComplete:^{
            }];
        }];
    };
    
    self.cancelHideTipView.sureBlock = ^{
        //        XMStrongSelf(self);
        [GKCover hideCover];
    };
    
    self.realTipView.realBlock = ^{
        XMStrongSelf(self);
        XMWeakSelf(self);
        [GKCover hideCoverWithHideBlock:^{
            XMStrongSelf(self);
            XMRealViewController *vc = [[XMRealViewController alloc] initWithNibName:kStringFromClass(XMRealViewController) bundle:nil];
            [self.navigationController pushViewController:vc animated:YES];
        }];
    };
    
    self.realTipView.closeBlock = ^{
        [GKCover hideCover];
    };
}

#pragma mark - Actions
- (void)more {
    [self xm_resignFirstResponder];
    [GKCover coverFrom:self.view contentView:self.moreView style:(GKCoverStyleTranslucent) showStyle:(GKCoverShowStyleCenter) showAnimStyle:(GKCoverShowAnimStyleNone) hideAnimStyle:(GKCoverHideAnimStyleNone) notClick:YES];
}

#pragma mark - Public Methods
#pragma mark - Privacy Methods

- (BOOL)checkBalanceEnough:(NSNumber *)price
                   message:(NSString *)message
                    isGift:(BOOL)isGift {
    if (isGift) {
        if (kUserModel.golds.integerValue < price.integerValue) {
            [MBProgressHUD showHUDMsg:message toView:nil completionBlock:^{
                [self toRecharge];
            }];
            return NO;
        }
    } else {
        if (kUserModel.golds.integerValue < price.integerValue && kUserModel.isMan) {
            [MBProgressHUD showHUDMsg:message toView:nil completionBlock:^{
                [self toRecharge];
            }];
            return NO;
        }
    }
    return YES;
}

- (void)toRecharge {
    XMRechargeViewController *vc = [[XMRechargeViewController alloc] initWithNibName:kStringFromClass(XMRechargeViewController) bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)reloadFreeMsgCount {
    self.chatSessionInputBarControl.inputTextView.placeholder = @"说点什么吧";
    //    if (kUserModel.free_chat_times.intValue > 0) {
    //        self.chatSessionInputBarControl.inputTextView.placeholder = kStringFormat(@"免费消息x%@", kUserModel.free_chat_times);
    //    } else {
    //        self.chatSessionInputBarControl.inputTextView.placeholder = @"说点什么吧";
    //    }
}

- (void)clearRecordAlert {
    [XMAlertHelper alertTitle:@"确定删除聊天记录吗?"
                      message:nil
                  cancelTitle:@"取消"
                    sureTitle:@"确定"
                 cancelAction:^{
    } sureAction:^{
        [self clearHistoryMessages];
    }];
}

- (void)clearHistoryMessages {
    [[RCIMClient sharedRCIMClient] clearHistoryMessages:self.conversationType
                                               targetId:self.targetId
                                             recordTime:0
                                            clearRemote:YES
                                                success:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD showHUDSucc:@"清除成功" toView:nil];
            [self.conversationDataRepository removeAllObjects];
            [self.conversationMessageCollectionView reloadData];
        });
    } error:^(RCErrorCode status){
        
    }];
}

- (void)setupHiding {
    [MBProgressHUD showHUDActivityMsg:nil];
    NSMutableDictionary *dic = @{
        @"target_user_id": self.userModel.user_id
    }.mutableCopy;
    
    [YHNetworkManager.sharedInstance postWithUrl:@"api/chat/invisible/edit" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        if (tip.success) {
            [MBProgressHUD hideHUD];
            if (self.userModel.is_hidden) {
                [MBProgressHUD showHUDSucc:@"已取消在线对其隐身" toView:kWindow];
                self.userModel.is_hidden = NO;
                [self.moreView.hideButton setTitle:@"在线对其隐身" forState:UIControlStateNormal];
            } else {
                [MBProgressHUD showHUDSucc:@"已设置在线对其隐身" toView:kWindow];
                self.userModel.is_hidden = YES;
                [self.moreView.hideButton setTitle:@"取消对其隐身" forState:UIControlStateNormal];
            }
        }
    }];
}

- (void)setupBlack {
    [MBProgressHUD showHUDActivityMsg:nil toView:kWindow];
    NSMutableDictionary *dic = @{
        @"target_user_id": self.userModel.user_id
    }.mutableCopy;
    [YHNetworkManager.sharedInstance postWithUrl:@"api/chat/blacklist/edit" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        if (tip.success) {
            [MBProgressHUD hideHUD];
            if (self.userModel.is_block) {
                [MBProgressHUD showHUDSucc:@"已将对方移除黑名单" toView:kWindow];
                self.userModel.is_block = NO;
                [self.moreView.blackButton setTitle:@"拉黑" forState:UIControlStateNormal];
            } else {
                [MBProgressHUD showHUDSucc:@"已将对方加入黑名单" toView:kWindow];
                self.userModel.is_block = YES;
                [self.moreView.blackButton setTitle:@"取消拉黑" forState:UIControlStateNormal];
            }
        }
    }];
}

#pragma mark - Super && 钩子
#pragma mark - 用户注册自定义消息的入口
- (void)registerCustomCellsAndMessages {
    [super registerCustomCellsAndMessages];
    [self registerClass:[XMCustomSendCell class] forMessageClass:[XMCustomSendMessage class]];
    [self registerClass:[XMSendInviteMsgCell class] forMessageClass:[XMInviteMessage class]];
}

#pragma mark - 即将显示消息Cell的回调
- (void)willDisplayMessageCell:(RCMessageBaseCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    if ([cell isKindOfClass:[XMSendInviteMsgCell class]]) {
        XMSendInviteMsgCell *msgCell = (XMSendInviteMsgCell *)cell;
#pragma mark - 邀请消息点击接受
        XMWeakSelf(self);
        msgCell.sendBlock = ^(RCMessageModel * _Nonnull msgModel) {
            XMInviteMessage *message = (XMInviteMessage *)msgModel.content;
            XMStrongSelf(self);
            switch (message.msgType.intValue) {
                case 1: {
                    if (![self checkBalanceEnough:message.golds message:kGlobalVariables.appConfig.chat.otherMsgTipStr isGift:YES]) { return; }
                    [self sendGift:message.index.stringValue
                      animationUrl:message.animUrl complete:^{
                        [self updateMessageExpansion:msgModel];
                    }];
                } break;
                case 2: {
                    [self openGuardDataComplete:^{
                        [self updateMessageExpansion:msgModel];
                    }];
                } break;
                case 3: {
                    [self openboxDataComplete:^{
                        [self updateMessageExpansion:msgModel];
                    }];
                } break;
                default:
                    break;
            }
        };
    }
    
    if ([cell isKindOfClass:[RCRichContentMessageCell class]]) {
        RCMessageCell *c = (RCMessageCell *)cell;
        c.showPortrait = NO;
    }
    
    [super willDisplayMessageCell:cell atIndexPath:indexPath];
}

- (void)updateMessageExpansion:(RCMessageModel *)model {
    [[RCCoreClient sharedCoreClient] updateMessageExpansion:@{@"status": @"1"} messageUId:model.messageUId success:^{
        NSUInteger row = [self.conversationDataRepository indexOfObject:model];
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:row inSection:0];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.conversationMessageCollectionView reloadItemsAtIndexPaths:@[indexPath]];
        });
    } error:^(RCErrorCode status) {
    }];
}

#pragma mark - 点击Cell
- (void)didTapMessageCell:(RCMessageModel *)model {
    RCMessageContent *_messageContent = model.content;
    if ([self isExtensionCell:_messageContent] &&
        [_messageContent isMemberOfClass:RCCallSummaryMessage.class]) {
        RCCallSummaryMessage *msg = (RCCallSummaryMessage *)_messageContent;
        if (kUserModel.isGirl) {
            if (!kUserModel.idcard_verify) {
                [self showRealTipView];
                return;
            }
            
            if (!self.userModel.rtc_status) {
                [MBProgressHUD showHUDMsg:msg.mediaType == RCCallMediaAudio ? kStringFormat(@"亲密度大于%ld才可以语音通话哦", self.rtcModel.intimacy) : kStringFormat(@"亲密度大于%ld才可以视频通话哦", self.rtcModel.intimacy) toView:nil];
                return;
            }
        }
        
        if (msg.mediaType == RCCallMediaAudio) {
            if (kUserModel.isMan) {
                if (![self checkBalanceEnough:@(self.rtcModel.voice_golds) message:kStringFormat(@"语音通话%@金币/分钟", @(self.rtcModel.voice_golds)) isGift:NO]) {
                    return;
                }
            }
            
        } else if (msg.mediaType == RCCallMediaVideo) {
            if (kUserModel.isMan) {
                if (![self checkBalanceEnough:@(self.rtcModel.video_golds) message:kStringFormat(@"对方设置视频通话为%@金币/分钟", @(self.rtcModel.video_golds)) isGift:NO]) {
                    return;
                }
            }
        }
    }
    
    if (![XMUtil isBlankString:_messageContent.extra]) {
        NSDictionary *extraDic = [_messageContent.extra jsonValueDecoded];
        NSString *nameStr = [extraDic safe_objectForKey:@"name"];
        NSString *linkStr = [extraDic safe_objectForKey:@"link"];
        
        if (![XMUtil isBlankString:nameStr] && ![XMUtil isBlankString:linkStr]) {
            XMWebViewController *vc = [[XMWebViewController alloc] init];
            vc.titleStr = nameStr;
            vc.urlStr = linkStr;
            vc.needNav = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
        return;
    }
    [super didTapMessageCell:model];
}

- (void)didTapCellPortrait:(NSString *)userId {
    if (self.conversationType == ConversationType_SYSTEM) {
        return;
    }
    if ([userId isEqualToString:kUserModel.user_id]) {
        return;
    }
    
    XMUserDetailViewController *vc = [[XMUserDetailViewController alloc] init];
    vc.userId = self.userModel.user_id;
    vc.fromChat = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 收到消息
- (void)didReceiveMessage:(RCMessage *)message leftDic:(NSDictionary *)leftDic {
    if ([message.content isKindOfClass:NSClassFromString(@"XMCustomSendMessage")]) {
        XMCustomSendMessage *msg = (XMCustomSendMessage *)message.content;
        if (msg.isVideo.intValue != 1 && [msg.animUrl containsString:@"svga"]) {
            [self showGiftSvgaPlayView:msg.animUrl finishBlock:^(SVGAPlayer *player) {
                [GKCover hideCover];
            }];
        }
    }
    [self userObtain];
}

- (RCMessageContent *)willSendMessage:(RCMessageContent *)messageContent {
    if (kUserModel.isGirl && !kUserModel.idcard_verify) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self showRealTipView];
        });
        return nil;
    }
    
    if ([messageContent isKindOfClass:RCTextMessage.class] ||
        [messageContent isKindOfClass:RCImageMessage.class] ||
        [messageContent isKindOfClass:RCHQVoiceMessage.class] ||
        [messageContent isKindOfClass:RCReferenceMessage.class] ||
        [messageContent isKindOfClass:RCGIFMessage.class]) {
        if (self.userModel.muted_minutes.intValue > 0) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD showHUDMsg:kStringFormat(@"已被禁言%@",self.userModel.muted_minutes) toView:nil];
            });
            return nil;
        }
    }
    
    return [super willSendMessage:messageContent];
}

- (void)sendMessage:(RCMessageContent *)messageContent pushContent:(NSString *)pushContent {
    if ([messageContent isKindOfClass:RCTextMessage.class] ||
        [messageContent isKindOfClass:RCImageMessage.class] ||
        [messageContent isKindOfClass:RCHQVoiceMessage.class] ||
        [messageContent isKindOfClass:RCReferenceMessage.class] ||
        [messageContent isKindOfClass:RCGIFMessage.class]) {
        NSNumber *msgPrice = @(0);
        XMChatMessageSendType type = 0;
        NSString *tipStr = @"";
        if ([messageContent isKindOfClass:RCTextMessage.class] ||
            [messageContent isKindOfClass:RCReferenceMessage.class]) {
            type = XMChatMessageSendText;
            msgPrice = kGlobalVariables.appConfig.chat.text_golds;
            tipStr = kGlobalVariables.appConfig.chat.textMsgTipStr;
        } else if ([messageContent isKindOfClass:RCImageMessage.class] ||
                   [messageContent isKindOfClass:RCGIFMessage.class]) {
            type = XMChatMessageSendImage;
            msgPrice = kGlobalVariables.appConfig.chat.picture_golds;
            tipStr = kGlobalVariables.appConfig.chat.imageMsgTipStr;
        } else if ([messageContent isKindOfClass:RCHQVoiceMessage.class]) {
            type = XMChatMessageSendVoice;
            msgPrice = kGlobalVariables.appConfig.chat.voice_golds;
            tipStr = kGlobalVariables.appConfig.chat.voiceMsgTipStr;
        }
        if (self.userModel.free_chat_times == 0) {
            if ([self checkBalanceEnough:msgPrice message:tipStr isGift:NO]) {
                [self xm_sendMessageType:type
                                complete:^(BOOL success) {
                    if (success) {
                        [super sendMessage:messageContent pushContent:pushContent];
                    }
                }];
            }
        } else {
            [self xm_sendMessageType:type
                            complete:^(BOOL success) {
                if (success) {
                    [super sendMessage:messageContent pushContent:pushContent];
                }
            }];
        }
    } else {
        [super sendMessage:messageContent pushContent:pushContent];
    }
}

#pragma mark - 发送消息完成的回调 发送状态，0表示成功，非0表示失败
- (void)didSendMessageModel:(NSInteger)status model:(RCMessageModel *)messageModel {
    if (status == 0) {
        if (self.userModel.is_hidden) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self xm_resignFirstResponder];
                [self showCancelHideTipView];
                [self userObtain];
            });
        } else {
            [self userObtain];
        }
        if ([messageModel.content isKindOfClass:RCTextMessage.class] ||
            [messageModel.content isKindOfClass:RCImageMessage.class] ||
            [messageModel.content isKindOfClass:RCHQVoiceMessage.class] ||
            [messageModel.content isKindOfClass:RCReferenceMessage.class] ||
            [messageModel.content isKindOfClass:RCGIFMessage.class]) {
            XMChatMessageSendType message_type = 0;
            if ([messageModel.content isKindOfClass:RCTextMessage.class] ||
                [messageModel.content isKindOfClass:RCReferenceMessage.class]) {
                message_type = XMChatMessageSendText;
            } else if ([messageModel.content isKindOfClass:RCImageMessage.class] ||
                       [messageModel.content isKindOfClass:RCGIFMessage.class]) {
                message_type = XMChatMessageSendImage;
            } else if ([messageModel.content isKindOfClass:RCHQVoiceMessage.class]) {
                message_type = XMChatMessageSendVoice;
            }
            NSMutableDictionary *dic = @{
                @"message_type": @(message_type),
                @"message_uid": [NSString xm_stringNoNullWith:messageModel.messageUId],
                @"message_time": @(messageModel.sentTime),
                @"target_user_id": [NSString xm_stringNoNullWith:messageModel.targetId],
            }.mutableCopy;
            [YHNetworkManager.sharedInstance postWithUrl:@"api/chat/message/handle" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
                if (tip.success) {
                    if (result[@"reply_message"]) {
                        [self.reply_message addObjectsFromArray:result[@"reply_message"]];
                        if (self.reply_message.count > 0) {
                            [self handlerReplyMessage];
                        }
                    }
                }
            }];
        }
    }
}

- (void)handlerReplyMessage {
    if (self.reply_message.count == 0) {
        return;
    }
    XMLog(@"reply_message_%@", self.reply_message);
    NSDictionary *dic = [self.reply_message firstObject];
    NSString *message_uid = [dic toString:@"message_uid"];
    [[RCCoreClient sharedCoreClient] updateMessageExpansion:dic messageUId:message_uid success:^{
        XMLog(@"更新扩展成功");
        [self.conversationDataRepository enumerateObjectsUsingBlock:^(RCMessageModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj.messageUId isEqualToString:message_uid]) {
                obj.cellSize = CGSizeZero;
                NSIndexPath *indexPath = [NSIndexPath indexPathForItem:idx inSection:0];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.conversationMessageCollectionView reloadItemsAtIndexPaths:@[indexPath]];
                });
                *stop = YES;
            }
        }];
        [self.reply_message removeFirstObject];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_global_queue(0, 0), ^{
            [self handlerReplyMessage];
        });
        
    } error:^(RCErrorCode status) {
        XMLog(@"更新扩展失败 ");
    }];
        
}

#pragma mark - 取消了消息发送的回调
- (void)didCancelMessage:(RCMessageContent *)messageContent {
}

- (XMConversationMoreView *)moreView {
    if (!_moreView) {
        _moreView = [XMConversationMoreView viewFromXib];
        _moreView.frame = kFRAME(0, 0, kScreenWidth, kScreenHeight);
    }
    return _moreView;
}

- (XMUserDetailRemarkView *)remarkView {
    if (_remarkView == nil) {
        _remarkView = [XMUserDetailRemarkView viewFromXib];
        _remarkView.type = XMEditUserInputTypeRemark;
        _remarkView.size = CGSizeMake(kScreenWidth, kScreenHeight);
    }
    return _remarkView;
}

- (XMGiftView *)giftView {
    if (_giftView == nil) {
        _giftView = [XMGiftView viewFromXib];
        _giftView.size = CGSizeMake(kScreenWidth, 382 + kBottomSafeHeight);
    }
    return _giftView;
}

- (void)showRemarkView {
    [GKCover coverFrom:self.view contentView:self.remarkView style:(GKCoverStyleTranslucent) showStyle:(GKCoverShowStyleCenter) showAnimStyle:(GKCoverShowAnimStyleNone) hideAnimStyle:(GKCoverHideAnimStyleNone) notClick:YES];
}

- (void)showGiftView {
    [self bagData];
    [self.giftView reloadGolod];
    [self.giftView resetViews];
    [GKCover coverFrom:self.view contentView:self.giftView style:(GKCoverStyleTranslucent) showStyle:(GKCoverShowStyleBottom) showAnimStyle:(GKCoverShowAnimStyleNone) hideAnimStyle:(GKCoverHideAnimStyleNone) notClick:NO];
}

- (XMConversationInviteView *)inviteView {
    if (!_inviteView) {
        _inviteView = [XMConversationInviteView viewFromXib];
        _inviteView.size = CGSizeMake(kScreenWidth, kScreenHeight);
    }
    return _inviteView;
}

#pragma mark - 各种邀请弹窗
- (void)showinviteViewType:(XMConversationInviteType)type {
    XMWeakSelf(self);
    [self.inviteView setupWithType:type sendBlock:^(XMConversationInviteType type) {
        XMStrongSelf(self);
        XMWeakSelf(self);
        [GKCover hideCoverWithHideBlock:^{
            XMStrongSelf(self);
            switch (type) {
                case XMConversationInviteGuard:
                    if (self.userModel.is_watch) {
                        [self showhasGuardView];
                    } else {
                        [self showOpenGuardTipView];
                    }
                    break;
                case XMConversationInviteGift:
                    self.giftView.isInvite = NO;
                    [self showGiftView];
                    break;
                case XMConversationInviteBox:
                    [self showOpenBoxView];
                    break;
                default:
                    break;
            }
        }];
        
    } inviteBlock:^(XMConversationInviteType type) {
        XMStrongSelf(self);
        XMWeakSelf(self);
        [GKCover hideCoverWithHideBlock:^{
            XMStrongSelf(self);
            switch (type) {
                case XMConversationInviteGuard: {
                    XMInviteMessage *message = [XMInviteMessage guardMessageWithName:@"开启守护" golds:kGlobalVariables.appConfig.chat.user_guard_golds score:@"" msgType:@(2)];
                    [self sendMessage:message pushContent:nil];
                }
                    break;
                case XMConversationInviteGift:
                    self.giftView.isInvite = YES;
                    [self showGiftView];
                    break;
                case XMConversationInviteBox: {
                    XMInviteMessage *message = [XMInviteMessage guardMessageWithName:@"开盲盒" golds:kGlobalVariables.appConfig.chat.blind_box_golds score:@"" msgType:@(3)];
                    [self sendMessage:message pushContent:nil];
                } break;
                default:
                    break;
            }
        }];
    }];
    
    [GKCover coverFrom:self.view contentView:self.inviteView style:(GKCoverStyleTranslucent) showStyle:(GKCoverShowStyleBottom) showAnimStyle:(GKCoverShowAnimStyleNone) hideAnimStyle:(GKCoverHideAnimStyleNone) notClick:NO];
}

- (XMSVGAPlayView *)svgaPlayView {
    if (_svgaPlayView == nil) {
        _svgaPlayView = [[XMSVGAPlayView alloc] initWithFrame:UIScreen.mainScreen.bounds];
    }
    return _svgaPlayView;
}

- (void)showGiftSvgaPlayView:(NSString *)urlStr
                 finishBlock:(void (^)(SVGAPlayer *player))finishBlock {
    if ([GKCover hasCover]) {
        [GKCover hideCoverWithHideBlock:^{
            [self showSvgaPlayView:urlStr finishBlock:^(SVGAPlayer *player) {
                finishBlock(player);
            }];
        }];
    } else {
        [self showSvgaPlayView:urlStr finishBlock:^(SVGAPlayer *player) {
            finishBlock(player);
        }];
    }
}

- (void)showSvgaPlayView:(NSString *)urlStr
             finishBlock:(void (^)(SVGAPlayer *player))finishBlock {
    [self.svgaPlayView playWithSource:urlStr
                          finishBlock:^(SVGAPlayer *_Nonnull player) {
        finishBlock(player);
    } faileBlock:^(NSError *_Nullable error) {
        [GKCover hideCover];
    }];
    [GKCover coverFrom:kWindow contentView:self.svgaPlayView style:GKCoverStyleTransparent showStyle:GKCoverShowStyleCenter showAnimStyle:GKCoverShowAnimStyleNone hideAnimStyle:GKCoverHideAnimStyleNone notClick:YES];
}

- (XMOpenGuardTipView *)openGuardTipView {
    if (_openGuardTipView == nil) {
        _openGuardTipView = [XMOpenGuardTipView viewFromXib];
        _openGuardTipView.size = CGSizeMake(kScreenWidth, kScreenHeight);
    }
    return _openGuardTipView;
}

- (void)showOpenGuardTipView {
    self.openGuardTipView.descLa.text = kStringFormat(@"确认消耗%@金币，增长%@亲密度，开启守护，成为彼此的天使，守护挚爱。", kGlobalVariables.appConfig.chat.user_guard_golds, kGlobalVariables.appConfig.chat.guardian_intimacy);
    
    [self.openGuardTipView.priceBtn setTitle:kStringFormat(@"%@金币", kGlobalVariables.appConfig.chat.user_guard_golds) forState:UIControlStateNormal];
    
    if ([GKCover hasCover]) {
        [GKCover hideCoverWithHideBlock:^{
            [GKCover coverFrom:self.view contentView:self.openGuardTipView style:GKCoverStyleTranslucent showStyle:GKCoverShowStyleCenter showAnimStyle:GKCoverShowAnimStyleNone hideAnimStyle:GKCoverHideAnimStyleNone notClick:YES];
        }];
    } else {
        [GKCover coverFrom:self.view contentView:self.openGuardTipView style:GKCoverStyleTranslucent showStyle:GKCoverShowStyleCenter showAnimStyle:GKCoverShowAnimStyleNone hideAnimStyle:GKCoverHideAnimStyleNone notClick:YES];
    }
}

- (XMCancelHideTipView *)cancelHideTipView {
    if (_cancelHideTipView == nil) {
        _cancelHideTipView = [XMCancelHideTipView viewFromXib];
        _cancelHideTipView.size = CGSizeMake(kScreenWidth, kScreenHeight);
    }
    return _cancelHideTipView;
}

- (void)showCancelHideTipView {
    if ([GKCover hasCover]) {
        [GKCover hideCoverWithHideBlock:^{
            [GKCover coverFrom:self.view contentView:self.cancelHideTipView style:GKCoverStyleTransparent showStyle:GKCoverShowStyleCenter showAnimStyle:GKCoverShowAnimStyleNone hideAnimStyle:GKCoverHideAnimStyleNone notClick:YES];
        }];
    } else {
        [GKCover coverFrom:self.view contentView:self.cancelHideTipView style:GKCoverStyleTransparent showStyle:GKCoverShowStyleCenter showAnimStyle:GKCoverShowAnimStyleNone hideAnimStyle:GKCoverHideAnimStyleNone notClick:YES];
    }
}

- (void)xm_resignFirstResponder {
    if ([self.chatSessionInputBarControl.inputTextView isFirstResponder]) {
        [self.chatSessionInputBarControl.inputTextView resignFirstResponder];
    }
}

- (XMConOpenBoxView *)openBoxView {
    if (_openBoxView == nil) {
        _openBoxView = [XMConOpenBoxView viewFromXib];
        _openBoxView.size = CGSizeMake(kScreenWidth, kScreenHeight);
    }
    return _openBoxView;
}

- (void)showOpenBoxView {
    if ([GKCover hasCover]) {
        [GKCover hideCoverWithHideBlock:^{
            [GKCover coverFrom:self.view contentView:self.openBoxView style:GKCoverStyleTranslucent showStyle:GKCoverShowStyleCenter showAnimStyle:GKCoverShowAnimStyleNone hideAnimStyle:GKCoverHideAnimStyleNone notClick:YES];
        }];
    } else {
        [GKCover coverFrom:self.view contentView:self.openBoxView style:GKCoverStyleTranslucent showStyle:GKCoverShowStyleCenter showAnimStyle:GKCoverShowAnimStyleNone hideAnimStyle:GKCoverHideAnimStyleNone notClick:YES];
    }
}

- (XMConOpenBoxResultView *)boxResultView {
    if (_boxResultView == nil) {
        _boxResultView = [XMConOpenBoxResultView viewFromXib];
        _boxResultView.size = CGSizeMake(kScreenWidth, kScreenHeight);
    }
    return _boxResultView;
}

- (void)showBoxResultView {
    if ([GKCover hasCover]) {
        [GKCover hideCoverWithHideBlock:^{
            [GKCover coverFrom:self.view contentView:self.boxResultView style:GKCoverStyleTranslucent showStyle:GKCoverShowStyleCenter showAnimStyle:GKCoverShowAnimStyleNone hideAnimStyle:GKCoverHideAnimStyleNone notClick:YES];
        }];
    } else {
        [GKCover coverFrom:self.view contentView:self.boxResultView style:GKCoverStyleTranslucent showStyle:GKCoverShowStyleCenter showAnimStyle:GKCoverShowAnimStyleNone hideAnimStyle:GKCoverHideAnimStyleNone notClick:YES];
    }
}

- (XMHasGuardTipView *)hasGuardView {
    if (_hasGuardView == nil) {
        _hasGuardView = [XMHasGuardTipView viewFromXib];
        _hasGuardView.size = CGSizeMake(kScreenWidth, kScreenHeight);
    }
    return _hasGuardView;
}

- (void)showhasGuardView {
    self.hasGuardView.intimacyStr = self.userModel.intimacy;
    if ([GKCover hasCover]) {
        [GKCover hideCoverWithHideBlock:^{
            [GKCover coverFrom:self.view contentView:self.hasGuardView style:GKCoverStyleTranslucent showStyle:GKCoverShowStyleCenter showAnimStyle:GKCoverShowAnimStyleNone hideAnimStyle:GKCoverHideAnimStyleNone notClick:YES];
        }];
    } else {
        [GKCover coverFrom:self.view contentView:self.hasGuardView style:GKCoverStyleTranslucent showStyle:GKCoverShowStyleCenter showAnimStyle:GKCoverShowAnimStyleNone hideAnimStyle:GKCoverHideAnimStyleNone notClick:YES];
    }
}

- (XMRealTipView *)realTipView {
    if (!_realTipView) {
        _realTipView = [XMRealTipView viewFromXib];
        _realTipView.size = CGSizeMake(kScreenWidth, kScreenHeight);
    }
    return _realTipView;
}

- (void)showRealTipView {
    [self xm_resignFirstResponder];
    if ([GKCover hasCover]) {
        [GKCover hideCoverWithHideBlock:^{
            [GKCover coverFrom:self.view contentView:self.realTipView style:GKCoverStyleTranslucent showStyle:GKCoverShowStyleCenter showAnimStyle:GKCoverShowAnimStyleNone hideAnimStyle:GKCoverHideAnimStyleNone notClick:YES];
        }];
    } else {
        [GKCover coverFrom:self.view contentView:self.realTipView style:GKCoverStyleTranslucent showStyle:GKCoverShowStyleCenter showAnimStyle:GKCoverShowAnimStyleNone hideAnimStyle:GKCoverHideAnimStyleNone notClick:YES];
    }
}

- (NSMutableArray *)reply_message {
    if (_reply_message == nil) {
        _reply_message = [NSMutableArray array];
    }
    return _reply_message;
}

- (void)dealloc {
    NSLog(@"释放了%@",[self class]);
    [kNotificationCenter removeObserver:self];
}

@end
